﻿using Common;
using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_ModifyPwd : Form
    {
        public Form_ModifyPwd()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void btn_save_Click(object sender, EventArgs e)
        {
            //var ope = tse.Operators.Where(u => u.OperatorID == 1).FirstOrDefault();
            var ope = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault();
            if (this.txt_Oldpass.Text == pm.DecryptString(ope.Password))
            {
                if (this.txt_Newpass.Text != null)
                {
                    if (this.txt_NewpassAgain.Text != null)
                    {
                        if (this.txt_Newpass.Text == this.txt_NewpassAgain.Text)
                        {
                            if (this.txt_Newpass.Text == this.txt_Oldpass.Text)
                            {
                                MessageBox.Show("新密码不能与旧密码一致");
                            }
                            else
                            {
                                ope.Password = pm.EncryptString(this.txt_Newpass.Text);
                                if (tse.SaveChanges() >= 1)
                                {
                                    MessageBox.Show("修改成功");
                                    this.Close();

                                }
                                else
                                {
                                    MessageBox.Show("修改失败");
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("两次输入的密码不一致");
                        }
                    }
                    else
                    {
                        MessageBox.Show("请重复新密码");
                    }
                }
                else
                {
                    MessageBox.Show("请输入新密码");
                }
            }
            else
            {
                MessageBox.Show("旧密码输入错误");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
